<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品id" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker
          v-model="daterangeOrderTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="车主id" prop="cargoUserName">
        <el-input
          v-model="queryParams.cargoUserName"
          placeholder="请输入车主id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否付款" prop="payOrNot">
        <el-select v-model="queryParams.payOrNot" placeholder="请选择是否付款" clearable>
          <el-option
            v-for="dict in dict.type.goods_pay"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否收货" prop="goodsOrNot">
        <el-select v-model="queryParams.goodsOrNot" placeholder="请选择是否收货" clearable>
          <el-option
            v-for="dict in dict.type.goods_received"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否取消" prop="cancelOrNot">
        <el-select v-model="queryParams.cancelOrNot" placeholder="请选择是否取消" clearable>
          <el-option
            v-for="dict in dict.type.goods_cancel"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shopping:commodityOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shopping:commodityOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shopping:commodityOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shopping:commodityOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commodityOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="id" />
      <el-table-column label="商品" align="center" prop="commodity.name" />
      <el-table-column label="商品图片" align="center" prop="commodityType" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.commodityType.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="count" />
      <el-table-column label="商品类型" align="center" prop="commodityType.type" />
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户" align="center" prop="cargoUser.realName" />
      <el-table-column label="收货地址" align="center" :formatter="formatAddress"  /> 
      <el-table-column label="是否付款" align="center" prop="payOrNot">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_pay" :value="scope.row.payOrNot"/>
        </template>
      </el-table-column>
      <el-table-column label="是否收货" align="center" prop="goodsOrNot">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_received" :value="scope.row.goodsOrNot"/>
        </template>
      </el-table-column>
      <el-table-column label="是否发货" align="center" prop="deliverOrNot">
        <template  slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.cancelOrNot == 1"
            >已取消</el-tag
          >
          <el-tag type="danger" v-else-if="scope.row.payOrNot == 0"
            >未付款</el-tag
          >
          <el-button
         v-else-if="scope.row.deliverOrNot == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handDeliver(scope.row)"
            v-hasPermi="['shopping:commodityOrder:edit']"
          >发货</el-button>
          <el-tag type="success" v-else-if="scope.row.deliverOrNot == 1"
            >已发货</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="是否取消" align="center" prop="cancelOrNot">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_cancel" :value="scope.row.cancelOrNot"/>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listCommodityOrder, getCommodityOrder, delCommodityOrder, addCommodityOrder, updateCommodityOrder } from "@/api/shopping/commodityOrder";

export default {
  name: "CommodityOrder",
  dicts: ['goods_cancel', 'goods_received','goods_pay'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品订单表格数据
      commodityOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否取消时间范围
      daterangeOrderTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        orderTime: null,
        cargoUserName: null,
        payOrNot: null,
        goodsOrNot: null,
        cancelOrNot: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatAddress(row, column, cellValue, index) {
      // 拼接需要显示的数据字段
      return `${row.commodityDeliveryAddress.province} ${row.commodityDeliveryAddress.city} ${row.commodityDeliveryAddress.area} ${row.commodityDeliveryAddress.detail}`;
    },
    /** 查询商品订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderTime && '' != this.daterangeOrderTime) {
        this.queryParams.params["beginOrderTime"] = this.daterangeOrderTime[0];
        this.queryParams.params["endOrderTime"] = this.daterangeOrderTime[1];
      }
      listCommodityOrder(this.queryParams).then(response => {
        this.commodityOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        commodityId: null,
        count: null,
        commodityTypeId: null,
        orderTime: null,
        cargoUserId: null,
        commodityDeliveryAddressId: null,
        payOrNot: null,
        goodsOrNot: null,
        cancelOrNot: null,
        del: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCommodityOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCommodityOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCommodityOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
     /** 发货按钮操作 */
     handDeliver(row) {
      row.deliverOrNot = 1
      updateCommodityOrder(row).then(response => {
              this.$modal.msgSuccess("发货成功");
              this.getList();
            });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品订单编号为"' + ids + '"的数据项？').then(function() {
        return delCommodityOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shopping/commodityOrder/export', {
        ...this.queryParams
      }, `commodityOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
